﻿#region Using Statements
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using Microsoft.Xna.Framework;
using JigLibX.Collision;
using JigLibX.Physics;
using JigLibX.Geometry;
using JigLibX.Math;
using Microsoft.Xna.Framework.Graphics;
using SqEngine.Components;
using sqengine.Components;
using sqengine.Engine.Common;
using SqEngine;
#endregion

namespace sqengine.PhysicObjects
{

    public class PowerUpObject : BoxObject
    {
        CollisionSkin _charactercs;
        HybridDictionary _params;
        PowerupObjectType _pot;
        Award awd = new Award();

        int _potvalue;
        bool ToRemove = false;

        public PowerUpObject(Game game, string ObjName, Model model, Vector3 sideLengths, Matrix orientation, Vector3 position, string filename, CollisionSkin CharacterCS, HybridDictionary Params)
            : base(game, ObjName, model, sideLengths, orientation, position, filename)
        {
            _charactercs = CharacterCS;
            _params = Params;
            _pot = (PowerupObjectType)(_params["PowerObjectType"]);
            _potvalue = (int)(_params["PowerObjectValue"]);

            SetColor(Color.White);


            body.CollisionSkin.callbackFn += new CollisionCallbackFn(PowerUpObject_OnCollision);
        }

        public bool PowerUpObject_OnCollision(CollisionSkin skin0, CollisionSkin skin1)
        {
            // skin0 will be MyObject's collision skin
            // skin1 will be the collision skin of the object that has collided with my object

            bool powcollision = false;
            string _playername = "";

            List<PK3_Model> bot_list = new List<PK3_Model>();

            bot_list = ((sqengine)CurrentGame).GetBotList;

            if (skin1 == _charactercs)
            {
                powcollision = CheckPowerUp(_pot, _potvalue, ((sqengine)CurrentGame).CurrentPlayer);
                _playername = ((sqengine)CurrentGame).CurrentPlayer.Name;

            }


            foreach (PK3_Model bot in bot_list)
            {
                CollisionSkin _cbs = new CollisionSkin();
                _cbs = bot.PhysicsBody.CollisionSkin;
                if (skin1 == _cbs)
                {
                    powcollision = CheckPowerUp(_pot, _potvalue, bot.BotPlayer);
                    _playername = bot.BotPlayer.Name;
                }

            }


            if (powcollision)
            {
                ToRemove = true;
                ((sqengine)(CurrentGame)).PlaySound("Audio\\m_health");
                ((sqengine)(CurrentGame)).AddAndPlayAward(awd);
                ((sqengine)(CurrentGame)).AddMessage(_playername + "> " + " take " + awd.Name, Color.Red, 1000, "");

            }

            return true;

        }

        private bool CheckPowerUp(PowerupObjectType type, int value, PlayerStats player)
        {
            PlayerStats _player = new PlayerStats(CurrentGame, 0, Vector3.Zero, "");

            _player = player;
             
            if (type == PowerupObjectType.LifePowerUp)
            {
                if (_player.MaxLife != _player.Life)
                {
                    if (_player.Life + _potvalue <= _player.MaxLife)
                    { _player.Life += _potvalue; }
                    else
                    { _player.Life = _player.MaxLife;}

                    player = _player;
                    awd.Name = _potvalue + " of life ";
                    awd.TextureAssetName = "Textures\\UI\\Awards\\life";
                    return true;
                }
            }
            else
                if (type == PowerupObjectType.ArmorPowerUp)
                {
                    if (_player.MaxArmor != _player.Armor)
                    {
                        if (_player.Armor + _potvalue <= _player.MaxArmor)
                        {
                            _player.Armor += _potvalue;
                        }
                        else
                        {
                            _player.Armor = _player.MaxArmor;
                        }

                        player = _player;
                        awd.Name = _potvalue + " of armor ";
                        awd.TextureAssetName = "Textures\\UI\\Awards\\armor";


                        return true;
                    }
                }
                else
                    if (type == PowerupObjectType.AmmoPowerUp)
                    {
                        if (_player.MaxAmmo != _player.Ammo)
                        {
                            if (_player.Ammo + _potvalue <= _player.MaxAmmo)
                            {
                                _player.Ammo += _potvalue;
                            }
                            else
                            {
                                _player.Ammo = _player.MaxAmmo;
                            }

                            player = _player;
                            awd.Name = _potvalue + " of ammo ";
                            awd.TextureAssetName = "Textures\\UI\\Awards\\Ammo";
                            return true;
                        }


                    }
            return false;
        }

        public override void Update(GameTime gameTime)
        {
            if (ToRemove)
            {
                body.DisableBody();
                this.Dispose();
                ((sqengine)CurrentGame).Components.Remove(this);
            }

            base.Update(gameTime);
        }

    }

}